home *** CD-ROM | disk | FTP | other *** search
- /** THE BRUTE FORCE FUNCTION !
- **
- ** For searching for text strings within binary files, or if you just
- ** don't LIKE the StrStr(); function. The logic for this was taken from
- ** an Algorithm book I found within University of Texas' Technical
- ** Library.
- **
- ** So when you gotta have that search... now you got it !
- **
- ** (courtesy of Dave Smith, CIS 71441,2723)
- **/
-
- #include <stdio.h>
- #include <string.h>
-
- int brute(char *a, char *p); /* Our function */
-
- void main(void)
- {
-
- char *shortee="store"; /* What we search for */
-
- /** Our test strings to search
- ** into
- **/
-
- char *longee="My father went to the store";
- char *longe2="The stora caught on fire !";
- char *longe3="Where's the #@!^^5*store&$@`~% ?";
-
- /* Our first test involves longee */
-
- if(brute(longee, shortee)) printf("\nFound %s in %s",shortee, longee);
- else printf("\nNo find");
-
- /* Longe2 has "STORA", which is similar, but not similar enough to "STORE" */
-
- if(brute(longe2, shortee)) printf("\nFound %s in %s",shortee, longe2);
- else printf("\nNo find 2");
-
- /** And finally, longe3 has "STORE" within it, closely surrounded
- ** by other characters.
- **/
-
- if(brute(longe3, shortee)) printf("\nFound %s in %s",shortee, longe3);
- else printf("\nNo find 3");
-
- }
-
-
-
- /*=================== Brute Force Function ==============================*/
- /*=======================================================================*/
- /*=========== Call with (LONGER STRING, SHORTER STRING) =================*/
-
- int brute(char *a, char *p)
- {
-
- int m, n, i=1, j=1; /* Set our positions to 1. */
-
- n=strlen(a)-1; /* N is the string length of longer string */
- m=strlen(p)-1; /* M is the string length of shorter string */
-
- /** If first chars match, increment in order to
- ** see if second, third, fourth... chars match.
- **/
-
- for(;;){
- if(a[i]==p[j]){
- i++;
- j++;
- }
-
- /** No, they don't match: restart shorter pointer,
- ** and point longer to next character.
- **/
-
- else{
- i=i-j+2;
- j=1;
- }
-
- if(j>m) return 1; /* We found a match, return with 1 */
-
-
- /** No match found and end-of-longer has been reached.
- ** Go home with 0.
- **/
-
- if(i>n) return 0;
-
- }
- }